<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use services\models\UpFileData;
use services\models\minisite\MActivityShow;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\MActivityShowSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '活动列表';
$this->params['breadcrumbs'][] = $this->title;


?>
<div class="mactivity-show-index">

   <div class="box box-success">
        <div class="box-body"><?php echo $this->render('_search', ['model' => $searchModel]); ?></div>
    </div>
    <div class="box box-success">
        <div class="box-body">
        <?php Pjax::begin(); ?>   
         <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'id',
            'c_sort',
            'c_name',
            [
            'attribute'=>'c_image',
            //'label' => ' 图片名称',
            'value'=>function  ($data) {
                 return Html::img(UpFileData::showImage($data->c_image),['width'=>'300','height'=>'80']) ;
            },
            'format'=>'html'
            ],
            //'c_begin',
            [  
               'attribute'=>'c_begin',
               'value'=>function($data) {
                    return $data->c_begin == '1970-01-01 00:00:00'  ? '未设置' : $data->c_begin;
                }
            ],
            //'c_end',
            [
                'attribute'=>'c_end',
                'value'=>function($data) {
                    return $data->c_end == '1970-01-01 00:00:00'  ? '未设置' : $data->c_end;
                }
            ],
            [
            'label'=>'状态',
            'value'=> function($data){
                 return MActivityShow::$STATUS[$data->c_status];
                }
            ],
            [
            'label'=>'',
            'value'=> function($data){
                 $typeArr = [0=>'上线',1=>'下线'];
                 return Html::a($typeArr[$data->c_status],'javascript:;',['class' => 'change-status btn btn-primary btn-sm']);
              },
            'format'=>'html'
            ],
            ['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
        ],
    ]); ?>
<?php Pjax::end(); ?>
        </div>
    </div>
</div>
<?php $this->beginBlock('js');?>
<script>
$(function(){
	$(".change-status").click(function(){
		var _this = this;
		var id = $(_this).parent().parent().attr('data-key');
		if(confirm('您确定执行当前操作吗？')){
			$.ajax({
                url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/change-status')?>',
                type: 'post',
                data: {id:id},
                dataType: 'json',
                success: function (e) {
                   if(e.status == 0) {
                       var btn_name = e.c_status == 1 ? "下线" : "上线" ;
                       var text_name = e.c_status == 0 ? "下线" : "上线" ;
                       
                       $(_this).text(btn_name);
                       $(_this).parent().prev('td').text(text_name);
                       alert(e.msg);
                       return false;
                   }else {
                       alert(e.msg);
                       return false;
                   }
                }
            });
		}
	});
})
</script>
<?php $this->endBlock();?>
